import http from './http'

export interface Post {
  id: number
  courseId: number
  courseName: string
  userId: number
  userName: string
  userAvatar?: string
  title: string
  content: string
  postType: 'QUESTION' | 'NOTE' | 'DISCUSSION'
  status: 'PUBLISHED' | 'CLOSED' | 'DELETED'
  isPinned: boolean
  isSolved: boolean
  viewCount: number
  likeCount: number
  commentCount: number
  createdAt: string
  updatedAt: string
  isLiked?: boolean
  canEdit?: boolean
  canDelete?: boolean
}

export interface Comment {
  id: number
  postId: number
  userId: number
  userName: string
  userAvatar?: string
  parentId?: number
  content: string
  likeCount: number
  isSolution: boolean
  createdAt: string
  updatedAt: string
  replies?: Comment[]
  isLiked?: boolean
  canMarkSolution?: boolean
}

export interface PostRequest {
  courseId: number
  title: string
  content: string
  postType: 'QUESTION' | 'NOTE' | 'DISCUSSION'
}

export interface CommentRequest {
  postId?: number
  parentId?: number
  content: string
}

export const communityApi = {
  // 创建帖子
  createPost: (data: PostRequest) => 
    http.post<Post>('/v1/community/posts', data),

  // 获取课程帖子列表
  getPostsByCourse: (courseId: number, params?: {
    postType?: string
    keyword?: string
    page?: number
    pageSize?: number
  }) => 
    http.get<{ content: Post[], totalElements: number }>(`/v1/community/courses/${courseId}/posts`, { params }),

  // 获取帖子详情
  getPost: (id: number) => 
    http.get<Post>(`/v1/community/posts/${id}`),

  // 更新帖子
  updatePost: (id: number, data: PostRequest) => 
    http.put<Post>(`/v1/community/posts/${id}`, data),

  // 删除帖子
  deletePost: (id: number) => 
    http.delete(`/v1/community/posts/${id}`),

  // 点赞帖子
  likePost: (id: number) => 
    http.post(`/v1/community/posts/${id}/like`),

  // 创建评论
  createComment: (postId: number, data: CommentRequest) => 
    http.post<Comment>(`/v1/community/posts/${postId}/comments`, data),

  // 获取帖子评论
  getComments: (postId: number, params?: {
    page?: number
    pageSize?: number
  }) => 
    http.get<{ content: Comment[], totalElements: number }>(`/v1/community/posts/${postId}/comments`, { params }),

  // 点赞评论
  likeComment: (id: number) => 
    http.post(`/v1/community/comments/${id}/like`),

  // 标记为解答
  markAsSolution: (id: number) => 
    http.post(`/v1/community/comments/${id}/solution`)
}

